#include "mycoin.h"
#include <qDebug>

// MyCoin::MyCoin(QWidget *parent)
//     : QWidget{parent}
// {}

MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    if (!pix.load(btnImg))
    {
        qDebug() << QString("图片 %1 加载失败").arg(btnImg);
        return;
    }
    setFixedSize(pix.width(), pix.height());
    setStyleSheet("QPushButton{border:0px;}");
    setIcon(pix);
    setIconSize(QSize(pix.width(), pix.width()));

    // 初始化定时器
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    // 监听正面翻反面的信号，并且翻转金币

    // 正面到反面
    connect(timer1, &QTimer::timeout, this, [=](){
        QPixmap pix;
        pix.load(QString(":res/Coin000%1.png").arg(min++));
        setFixedSize(pix.width(), pix.height());
        setStyleSheet("QPushButton{border:0px;}");
        setIcon(pix);
        setIconSize(QSize(pix.width(), pix.width()));
        if (min > max)
        {
            min = 1;
            timer1->stop();
            isAnimation = false;
        }
    });

    // 反面到正面
    connect(timer2, &QTimer::timeout, this, [=](){
        QPixmap pix;
        pix.load(QString(":res/Coin000%1.png").arg(max--));
        setFixedSize(pix.width(), pix.height());
        setStyleSheet("QPushButton{border:0px;}");
        setIcon(pix);
        setIconSize(QSize(pix.width(), pix.width()));
        if (max < min)
        {
            max = 8;
            timer2->stop();
            isAnimation = false;
        }
    });
}

void MyCoin::changeFlag()
{
    if (flag)
    {
        timer1->start(30); // 正面到反面
    }
    else
    {
        timer2->start(30); // 反面到正面
    }
    flag = !flag;
    isAnimation = true;
}

void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if (isAnimation || isWin)return;
    else
    {
        QPushButton::mousePressEvent(e);
    }
}
